package com.project.cotroller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("cookie")
public class CookieController {

    @RequestMapping("/test")
    public String testCookie(HttpServletResponse response){
        //创建cookie对象
        Cookie cookie = new Cookie("name","123");
        cookie.setMaxAge(100);
        //向响应信息中添加cookie信息
        response.addCookie(cookie);

        return "/html/cookie.html";
    }

    @RequestMapping("/info")
    @ResponseBody
    public String getInfo(HttpServletRequest request){
        Cookie[] cookieArray = request.getCookies();
        String str = "cookie信息";
        for(Cookie c : cookieArray){
            str += c.getName() + ":" + c.getValue() + "<br>";
        }
        return "ok";
    }

    @RequestMapping("/str")
    @ResponseBody
    public String getString(HttpServletResponse response){
        response.addCookie(new Cookie("grade","90"));
        return "str";
    }
}
